package com.chainyoung.component_tmperson.ui.mvp.model;

import com.chainyoung.common.appcommon.hdwallet.EtherscanService;
import com.chainyoung.common.appcommon.hdwallet.TokenBalance;
import com.chainyoung.common.integration.IRepositoryManager;
import com.chainyoung.common.mvp.BaseModel;
import com.chainyoung.component_tmperson.ui.mvp.contract.HdWalletTokenTransactionContract;

import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;

public class HdWalletTokenTransactionModel extends BaseModel implements HdWalletTokenTransactionContract.Model {


    @Inject
    HdWalletTokenTransactionModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);

    }


    @Override
    public Flowable<String> getSingleTokenTransfers(String contractAddr,String address,int page,int offset) {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .getSingleTokenTransfers(contractAddr,address,page,offset)
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody body) throws Exception {
                        return new JSONObject(body.string()).getString("result");
                    }
                });
    }


    @Override
    public Flowable<TokenBalance> getTokenBalance(final String contractAddr, String addr) {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .getTokenBalance(contractAddr, addr)
                .map(new Function<ResponseBody, TokenBalance>() {
                    @Override
                    public TokenBalance apply(@NonNull ResponseBody body) throws Exception {
                        return new TokenBalance(contractAddr,new JSONObject(body.string()).getString("result"));
                    }
                });
    }

}




